package com.hsy.bibc.modules.common;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.bibc.R;

/**
 * Created by Administrator on 2016/4/19.
 */
public class TimeCount extends CountDownTimer {

    private Context mContext;

    public boolean isRunning = false;

    private TextView code;

    private static final long TIMER_LENGTH = 60000l;
    private static final long TIMER_SEQUENCE = 1000l;

    public static TimeCount getInstance(TextView e, Context context) {
        TimeCount timeCount = new TimeCount(TIMER_LENGTH, TIMER_SEQUENCE, e, context);
        return timeCount;
    }

    public TimeCount(long millisInFuture, long countDownInterval, TextView e, Context context) {
        super(millisInFuture, countDownInterval);
        this.code = e;
        this.mContext = context;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (millisUntilFinished / 1000 == 0) {
            onFinish();
        } else {

            isRunning = true;
            String sendAgain = mContext.getString(R.string.get_code_again);
            code.setText(sendAgain + "(" + millisUntilFinished / 1000 + "s" + ")");
            DPLogUtil.d("time=" + millisUntilFinished / 1000);
            code.setClickable(false);
        }
    }

    @Override
    public void onFinish() {
        isRunning = false;
        String str = mContext.getString(R.string.get_code);
        code.setText(str);
        code.setClickable(true);
    }
}
